<?php

// Error reporting:
error_reporting(E_ALL^E_NOTICE);

// Including the DB connection file:
require 'common/connectDB.php';

$extension='';
$files_array = array();


/* Opening the thumbnail directory and looping through all the thumbs: */

$dir_handle = @opendir($directory) or die("There is an error with your file directory!");

while ($file = readdir($dir_handle))  {
	/* Skipping the system files: */
	if($file{0}=='.') continue;
	
	/* end() returns the last element of the array generated by the explode() function: */
	$extension = strtolower(end(explode('.',$file)));
	
	/* Skipping the php files: */
	if($extension == 'php') continue;

	$files_array[]=$file;
}
/* Sorting the files alphabetically */
sort($files_array,SORT_STRING);

$file_downloads=array();

$result = mysql_query("SELECT * FROM download_manager");

if(mysql_num_rows($result))
while($row=mysql_fetch_assoc($result))
{
	/* 	The key of the $file_downloads array will be the name of the file,
		and will contain the number of downloads: */
		
	$file_downloads[$row['filename']]=$row['downloads'];
}

?>
<html>
<head></head>

<body>

<table>
	<tr>
		<td>Id | </td>
		<td>Game's Name | </td>
		<td>Description</td>
	</tr>
	<?php
		while($game = mysql_fetch_array($games))
		{
			echo '<tr>
				<td><a href="index.php?game='.$game["game_id"].'">'.$game["game_id"].'</a></td>
				<td>'.$game["game_name"].'</td>
				<td>'.$game["game_description"].'</td>';
			foreach($files_array as $key=>$val) {
				$fileName = explode(".",$val);
				if($fileName[0] == $game["game_name"]) {
					echo '
						<td>
							<a href="downloadFile.php?file='.urlencode($val).'">download</a>
						</td>
					</tr>';
				}
			}
		}

	?>
</table>

</body>
</html>